import cv2
from loguru import logger

logger.add('D:\Setup.log')
class RGB2Gray:
    def average(img):
        logger.debug('Start average')
        height, width, td = img.shape
        for i in range(height):
            for j in range(width):
                Gray = round((int(img[i,j,0]) + int(img[i,j,1]) + int(img[i,j,2])) / 3)
                img[i,j] = [Gray,Gray,Gray]
        logger.debug('End average')
        return img
    def LightSensitivityAverage(img):
        logger.debug('Start LightSensitivityAverage')
        height, width, td = img.shape
        for i in range(height):
            for j in range(width):
                Gray = round(int(img[i,j,0] * 0.299 + int(img[i,j,1] * 0.587 + int(img[i,j,2]) * 0.144)))
                img[i,j] = [Gray,Gray,Gray]
        logger.debug('End LightSensitivityAverage')
        return img
    def OnlyGreen(img):
        logger.debug('Start OnlyGreen')
        height, width, td = img.shape
        for i in range(height):
            for j in range(width):
                Gray = round(int(img[i,j,1] * 0.587 ))
                img[i,j] = [Gray,Gray,Gray]
        logger.debug('End OnlyGreen')
        return img

if __name__ == '__main__':
    img = cv2.imread(r"D:\FFOutput\JXFLS.jpg",cv2.IMREAD_UNCHANGED)
    cv2.imshow('From',img)
    cv2.imshow('average',RGB2Gray.average(img))
    cv2.imshow('LightSensitivityAverage',RGB2Gray.LightSensitivityAverage(img))
    cv2.imshow('OnlyGreen',RGB2Gray.OnlyGreen(img))
    cv2.waitKey(0)
    cv2.destroyAllWindows()